﻿using System.Collections.Generic;
using System.IO;

namespace Note.Model
{
    class NoteModel
    {
        public static string NOTE_FOLDER = ConfigModel.ExtractPath;
        public string Type { get; set; }

        public List<NoteDetailModel> Notes { get; set; } = new List<NoteDetailModel>();

        public static List<NoteModel> NoteList { get; private set; } = new List<NoteModel>();

        static NoteModel()
        {
            LoadData();
        }

        private static void LoadData()
        {
            if (!Directory.Exists(NOTE_FOLDER))
            {
                Directory.CreateDirectory(NOTE_FOLDER);
            }
            foreach (var folder in Directory.GetDirectories(NOTE_FOLDER))
            {
                var type = Path.GetFileName(folder);
                var noteModel = new NoteModel()
                {
                    Type = type
                };
                var path = string.Format(@"{0}\{1}", NOTE_FOLDER, type);
                foreach (var file in Directory.GetFiles(path))
                {
                    var noteDeatailModel = new NoteDetailModel
                    {
                        Title = Path.GetFileName(file),
                        Type = type
                    };
                    noteModel.Notes.Add(noteDeatailModel);
                }
                NoteList.Add(noteModel);
            }
        }

        public static void RemoveNoteDetailToList(NoteDetailModel detailModel)
        {
            foreach (var model in NoteList)
            {
                if (model.Type == detailModel.Type)
                {
                    model.Notes.Remove(detailModel);
                    return;
                }
            }
        }
        public static void AddNoteDetailToList(NoteDetailModel detailModel)
        {
            foreach (var model in NoteList)
            {
                if (model.Type == detailModel.Type)
                {
                    model.Notes.Add(detailModel);
                    return;
                }
            }
            var noteModel = new NoteModel
            {
                Type = detailModel.Type
            };
            noteModel.Notes.Add(detailModel);
            NoteList.Add(noteModel);
        }
        public static NoteModel GetNodeDetailList(string type)
        {
            foreach (var noteModel in NoteList)
            {
                if (noteModel.Type == type)
                {
                    return noteModel;
                }
            }
            return null;
        }
    }
}
